package practice_0527;

public class practice01 {
    public static void main(String[] args) {
//        ticket ticket = new ticket();
//        new Thread(ticket).start();
//        new Thread(ticket).start();
//        new Thread(ticket).start();
        ticket2 ticket2 = new ticket2();
        new Thread(ticket2).start();
        new Thread(ticket2).start();
        new Thread(ticket2).start();


    }
}


//class ticket  implements Runnable{
//    private static int ticket_count = 100;
//    public  void sell() throws InterruptedException {
//        while(true) {
//
//                if (ticket_count <= 0) {
//                    System.out.println("售票结束....");
//                    break;
//                }
//                System.out.println(Thread.currentThread().getName() + "卖了一张票，剩余票数：" + --ticket_count);
//                Thread.sleep(50);
//            }
//
//
//    }
//
//
//
//    @Override
//    public void run() {
//        try {
//            sell();
//        } catch (InterruptedException e) {
//            throw new RuntimeException(e);
//        }
//    }
//}

class ticket2  extends Thread{
    private static int ticket_count = 100;
    final Object obj = new Object();
    public synchronized static void m2(){

    }
    public static void m3(){
        synchronized (ticket2.class){
            System.out.println("aaaaa");

        }
    }

    public void sell() throws InterruptedException {
        while(true) {
            synchronized (obj) {
                if (ticket_count <= 0) {
                    System.out.println("售票结束....");
                    break;
                }
                System.out.println(Thread.currentThread().getName() + "卖了一张票，剩余票数：" + --ticket_count);
                Thread.sleep(50);
            }
        }

    }



    @Override
    public void run() {
        try {
            sell();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

